package com.qk.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.code.BusinessEnum;
import com.qk.dto.course.CourseQueryDTO;
import com.qk.exception.BusinessException;
import com.qk.management.annotation.LogAnno;
import com.qk.result.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import com.qk.vo.course.CourseQueryVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:zhengbowen
 * @Date: 2025/10/06 16:18
 * @Description:课程管理业务逻辑
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CourseServiceImpl implements CourseService {
    private final CourseMapper courseMapper;

    @Override
    public PageResult<CourseQueryVO> selectCoursesByPage(CourseQueryDTO courseQueryDTO) {
        PageHelper.startPage(courseQueryDTO.getPage(), courseQueryDTO.getPageSize());
        List<CourseQueryVO> courseList = courseMapper.selectCoursesByPage(courseQueryDTO);
        PageInfo<CourseQueryVO> pageInfo = new PageInfo<>(courseList);
        return PageResult.<CourseQueryVO>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getList())
                .build();
    }

    @Override
    public Course selectCourseById(Integer id) {
        return courseMapper.selectCourseById(id);
    }

    @Override
    @LogAnno
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);
    }

    @Override
    @LogAnno
    public void insertCourse(Course course) {
        if(course.getPrice() == null||course.getPrice()<=0){
            throw new BusinessException(BusinessEnum.COURSE_PRICE_ERROR);
        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insertCourse(course);
    }

    @Override
    @LogAnno
    public void deleteCourse(Integer id) {
        courseMapper.deleteCourse(id);
    }

    @Override
    public List<Course> selectCoursesBySubject(Integer subject) {
        return courseMapper.selectCoursesBySubject(subject);
    }

    @Override
    public List<Course> selectCourses() {
        return courseMapper.selectCourses();
    }
}
